package com.cheung.leetcode.arr;

import org.junit.jupiter.api.Test;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.arr
 * @Author :  cheungming
 * @CreateTime : 2024-08-22 15:10:30
 * @Description : 1991
 * @Version : 1.0
 */
public class Code1991Test {

    public int pivotIndex(int[] nums) {
        // 求解逻辑，nums数组的和为s，中间数为nums[i],则有2(nums[0]+nums[1]+...+nums[i-1])+nums[i]=s
        // 2(nums[0]+nums[1]+...+nums[i-1]) == s- nums[i];
        int s = 0;
        for (int num : nums) {
            s += num;
        }

        int leftS = 0;

        for (int i = 0; i < nums.length; i++) {
            if (2 * leftS == s - nums[i]) {
                return i;
            }
            leftS += nums[i];
        }

        return -1;
    }


    @Test
    public void test1() {
        int[] nums = {1, 7, 3, 6, 5, 6};
        assert pivotIndex(nums) == 3;
    }


    @Test
    public void test2() {
        int[] nums = {2, 1, -1};
        assert pivotIndex(nums) == 0;
    }

    @Test
    public void test3() {
        int[] nums = {1, -1, 4};
        assert pivotIndex(nums) == 2;
    }


}
